package com.yanqu.road.server.cmd.activity.tonbingchannel;


import com.yanqu.road.entity.activity.tonbingchannel.config.ChannelTonBingConfig;
import com.yanqu.road.entity.enums.activity.tonbing.eTonBingStage;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChannelTonBingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;

import com.yanqu.road.server.manger.activity.tonbingchannel.ChannelTonBingMgr;
import com.yanqu.road.server.protocol.*;

@Cmd(code = GameProtocol.S_CTON_BING_SET_TARGET_CITY, desc = "设置战略目标")
public class TonBingSetTargetCityCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ChannelTonBingConfig config = ChannelTonBingMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        // 时间判断(活动期和休战期可操作)
        eTonBingStage stage = config.getTonBingStage();
        if (stage != eTonBingStage.Stage1Time && stage != eTonBingStage.Stage1RestTime) {
            ret = GameErrorCode.E_TON_BING_NO_OPERATE_TIME;
            return;
        }

        if (!ChannelTonBingMgr.isAble(player.getUserId())) {
            ret = GameErrorCode.E_TON_BING_NO_ABLE;
            return;
        }

        ChannelTonBingProto.CTonBingSetTargetCityReqMsg reqMsg = ChannelTonBingProto.CTonBingSetTargetCityReqMsg.parseFrom(packet.getBytes());

        ChannelTonBingProto.ChannelTonBingSetTargetCityReqMsg.Builder builder = ChannelTonBingProto.ChannelTonBingSetTargetCityReqMsg.newBuilder();
        builder.setActivityId(config.getActivityInfo().getActivityId());
        builder.setReq(reqMsg.toBuilder());
        player.sendChannelPlayerPacket(ChannelProtocol.N_TON_BING_SET_TARGET_CITY, builder);

    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            ChannelTonBingProto.CTonBingSetTargetCityRespMsg.Builder respMsg = ChannelTonBingProto.CTonBingSetTargetCityRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_CTON_BING_SET_TARGET_CITY, respMsg);
        }
    }
}
